var but=document.getElementById("free_download");
var box=document.createElement("div");
box.style.backgroundColor="#333";
box.style.color="#FFFFFF";
box.style.width="100%";
box.style.height="100%";
box.style.position="absolute";
box.style.top="0";box.style.left="0";box.style.textAlign="center";box.style.fontSize="12px";box.style.fontFamily="'Lucida Grande', Arial, Helvetica, sans-serif";box.style.zIndex="10000";document.body.appendChild(box);var logo=document.createElement("div");logo.style.paddingTop="50px";logo.align='center';box.appendChild(logo);logo.innerHTML="<img src='http://www.pelimaniaco.com/application/images/pelimaniaco-logo.png' />";var box2=document.createElement("div");box2.style.paddingTop="50px";box.appendChild(box2);document.body.style.overflow="hidden";

if(document.getElementById('downloadFileInfo').innerHTML.match(/Sorry, this file has been removed/i)){
	box2.innerHTML="El archivo ha sido eliminado de Wupload.";
}else if(but){
	box2.appendChild(but);but.innerHTML="Click para cargar el video";
	styleButton(but)
	if(but.addEventListener){
		but.addEventListener("click",clickBut,false);}else{but.attachEvent("onclick",clickBut);
	}
}

function clickBut(e){
	if(but.removeEventListener){
		but.removeEventListener("click",clickBut,false);
	}else{
		but.detachEvent("onclick",clickBut);
	}
	box2.innerHTML="Cargando...";
	setTimeout("getCaptcha()",200);
}

function getCaptcha(){
	if(document.getElementById("countdown")){
		var m=parseInt(document.getElementById("countdown").innerHTML);
		if(m>0){
			box2.innerHTML="Por favor, espera "+document.getElementById("countdown").innerHTML;
		}else{
			box2.innerHTML="Cargando...";
			setTimeout("getCaptcha()",500);
			return;
		}
	}else if(document.getElementById("captchaForm")){
		var form=document.getElementById("captchaForm");
		window.scrollTo(0,0)
		box2.innerHTML="";
		var n=document.createElement('div');
		n.style.position="fixed";
		n.style.top="180px";
		n.style.zIndex="10001";
		n.style.width='442px';
		n.style.margin='0 auto';
		document.body.appendChild(n);
		var m=document.getElementById('downloadMode');
		n.appendChild(m);
		var head=document.getElementsByTagName('head')[0], 
			style=document.createElement('style'),
			rules=document.createTextNode('.captchaMessage { display: none; }');
		style.type='text/css';
		if(style.styleSheet)
			style.styleSheet.cssText=rules.nodeValue;
		else 
			style.appendChild(rules);
		head.appendChild(style);
		var n=form.childNodes.length;
		var a=form.childNodes[n-2];
		a.value="Enviar captcha";
		styleButton(a)
		setTimeout("getFinalLink()",500);
		return;
	}else if(document.getElementById("downloadLink")){
		getFinalLink();
		return;
	}else if(document.getElementById('downloadErrors')){
		if(document.getElementById('downloadErrors').innerHTML.match(/You can only download 1 file at a time/i)){
			box2.innerHTML="Ya estás descargando un archivo desde Wupload. <br />No puedes cargar más de 1 archivo en simultáneo.";
			return;
		}
	}
	setTimeout("getCaptcha()",500);
}

function getFinalLink(){
	var p=document.getElementById("downloadLink");
	if(p){
		var n=p.childNodes.length;
		a=p.childNodes[n-2].childNodes[0];
		var hashes=window.location.href.slice(window.location.href.indexOf('?')+ 1).replace(/amp;/gi,'');
		
		window.location = 'http://www.pelimaniaco.com/player/play/?link='+urlencode(a.href)+'&count=10&'+hashes;
		return;
	}
	setTimeout("getFinalLink()",500);
}
	
function getElementByClass(theClass){
	var allHTMLTags=document.getElementsByTagName("*");
	for(i=0;i<allHTMLTags.length;i++){
		if(allHTMLTags[i].className==theClass){
			return allHTMLTags[i];
		}
	}
	return false;
}

function styleButton(b){
	b.className='';
	b.style.background="#19AE6A";
	b.style.borderRadius='4px';
	b.style.MozBorderRadius='4px';
	b.style.fontWeight="bold";
	b.style.padding="8px 15px";
	b.style.color="#FFFFFF";
	b.style.textDecoration="none";
	b.style.display="inline";
	b.style.fontWeight='bold';
	b.style.border=0;
	b.style.fontSize='11px';
	b.style.fontFamily="'Lucida Sans Unicode','Lucida Grande',sans-serif";
	b.style.cursor='pointer';
	b.style.fontStyle='normal';
}


function urlencode( str ) {
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}